#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* partition(struct ListNode* head, int x) {
    if (head == NULL)
        return NULL;
    if (head->next == NULL)
        return head;

    struct ListNode* big = head, * small = head;
    int tmp = 0;


    while (big->next)
    {
        if (big->val >= x)
        {
            small = big->next;
            while (small->val >= x && small->next)
            {
                small = small->next;
            }
            if (small->val < x)
            {
                tmp = big->val;
                big->val = small->val;
                small->val = tmp;
            }
            else
                return head;
        }
        big = big->next;
    }
    return head;

}